package TextRank;

import org.lionsoul.jcseg.ISegment;
import org.lionsoul.jcseg.dic.ADictionary;
import org.lionsoul.jcseg.dic.DictionaryFactory;
import org.lionsoul.jcseg.extractor.SummaryExtractor;
import org.lionsoul.jcseg.extractor.impl.TextRankSummaryExtractor;
import org.lionsoul.jcseg.segmenter.SegmenterConfig;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

public class TextRank {
    public List<String> TextRank(String str,int linenum) throws IOException {
        //1, 创建Jcseg ISegment分词对象
        SegmenterConfig config = new SegmenterConfig(true);
        config.setClearStopwords(true);     //设置过滤停止词
        config.setAppendCJKSyn(false);      //设置关闭同义词追加
        config.setKeepUnregWords(false);    //设置去除不识别的词条
        ADictionary dic = DictionaryFactory.createSingletonDictionary(config);
        ISegment seg = ISegment.COMPLEX.factory.create(config, dic);

        //2, 构造TextRankSummaryExtractor自动摘要提取对象
        SummaryExtractor extractor = new TextRankSummaryExtractor(seg, new SelfSeg());

        //3, 从一个Reader输入流中获取length长度的摘要
//        String str = "Jcseg是基于mmseg算法的一个轻量级开源中文分词器，同时集成了关键字提取，关键短语提取，关键句子提取和文章自动摘要等功能，并且提供了最新版本的lucene,%20solr,%20elasticsearch的分词接口。Jcseg自带了一个%20jcseg.properties文件用于快速配置而得到适合不同场合的分词应用。例如：最大匹配词长，是否开启中文人名识别，是否追加拼音，是否追加同义词等！";
//        String str = "import org.lionsoul.jcseg.ISegment;import org.lionsoul.jcseg.dic.ADictionary;import org.lionsoul.jcseg.dic.DictionaryFactory;import org.lionsoul.jcseg.extractor.SummaryExtractor;import org.lionsoul.jcseg.extractor.impl.TextRankSummaryExtractor;import org.lionsoul.jcseg.segmenter.SegmenterConfig;import org.lionsoul.jcseg.sentence.SentenceSeg;";
//        String summary = extractor.getSummary(new StringReader(str), 64);
        //4, output:
        //Jcseg是基于mmseg算法的一个轻量级开源中文分词器，同时集成了关键字提取，关键短语提取，关键句子提取和文章自动摘要等功能，并且提供了最新版本的lucene, solr, elasticsearch的分词接口。

        //5, 从一个Reader输入流中提取n个关键句子
        ((TextRankSummaryExtractor) extractor).setSentenceNum(linenum);        //设置返回的关键句子个数
        List<String> keySentences = extractor.getKeySentence(new StringReader(str));
        //调整代码次序
        Order or = new Order();
        List<String> Sentences = or.Order(str,keySentences);
        return Sentences;

    }
}

